import { Entity, Column, PrimaryGeneratedColumn, ManyToOne, BeforeInsert } from 'typeorm';
import { ApiProperty } from '@nestjs/swagger';
import { School } from 'src/modules/school/entities/school.entity';
import { Major } from 'src/modules/major/entities/major.entity';
import { Teacher } from 'src/modules/teacher/entities/teacher.entity';
import { Exclude } from 'class-transformer';
import { Gender, Status } from 'src/dictionaries/index.enum';
import { hashSync } from 'bcryptjs';
import { BaseEntity } from 'src/common/entities/entities';

@Entity({ name: 'user' })
export class User extends BaseEntity {

  @ApiProperty({ example: 'email', description: '用户邮箱' })
  @Column({
    type: 'varchar',
    comment: '用户姓名'
  })
  email: string;

  /** 密码 */
  @ApiProperty({ example: 'password', description: '密码' })
  @Exclude()
  @Column({
    type: 'varchar',
    length: 100,
    comment: '密码',
    select: false,
  })
  password: string;

  @BeforeInsert()
  async encryptPwd() {
    this.password = await hashSync(this.password);
  }

  @ApiProperty({ example: 'username', description: '用户姓名' })
  @Column({
    type: 'varchar',
    comment: '用户姓名'
  })
  username: string;

  @ApiProperty({ example: 'hometown', description: '用户籍贯' })
  @Column({
    type: 'varchar',
    comment: '用户籍贯'
  })
  hometown: string;

  @ApiProperty({ example: 'address', description: '用户地址' })
  @Column({
    type: 'varchar',
    comment: '用户地址'
  })
  address: string;

  @ApiProperty({ example: 'ip', description: '用户IP地址' })
  @Column({
    type: 'varchar',
    comment: '用户IP地址'
  })
  ip: string;

  @ApiProperty({ example: 'phone', description: '用户手机号' })
  @Column({
    type: 'varchar',
    comment: '用户手机号'
  })
  phone: string;

  /** 用户头像 */
  @ApiProperty({ example: 'avatar', description: '用户头像' })
  @Column({
    type: 'text',
    comment: '用户头像',
    nullable: true,
  })
  avatar: string;

  /** 账号 */
  @ApiProperty({ example: 'account ', description: '账号' })
  @Column({
    type: 'varchar',
    length: 100,
    comment: '账号',
    unique: true,
  })
  account: string;

  /** 登录时间 */
  @ApiProperty({ example: 'loginDate', description: '登录时间' })
  @Column({
    type: 'datetime',
    comment: '登录时间',
    nullable: true,
  })
  loginDate: Date;

  @ApiProperty({ enum: Status, example: Status.ENABLED, description: '用户状态' })
  @Column({
    type: 'enum',
    enum: Status,
    default: Status.ENABLED,
  })
  status: Status;

  /** 性别*/
  @ApiProperty({
    enum: Gender,
    example: Gender.MALE,
    description: '性别',
  })
  @Column({
    comment: '性别',
    type: 'enum',
    enum: Gender,
    nullable: true
  })
  gender: string;

  // @ApiProperty({ example: '所属学校实体', description: '所属学校' })
  // @ManyToOne(() => School, school => school.users)
  // school: School;

  @ApiProperty({ example: '所属专业实体', description: '所属专业' })
  @ManyToOne(() => Major, major => major.users)
  major: Major;

  @ApiProperty({ example: '导师实体', description: '导师' })
  @ManyToOne(() => Teacher, teacher => teacher.students)
  advisor: Teacher;

}
